package com.example.ch02_student.adminsDo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ch02_student.R;
import com.example.ch02_student.entity.Teacher;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TeacherUpdateInfo extends AppCompatActivity {
    private EditText no, name, sex1, age1, tel1;
    private OkHttpClient okHttpClient;
    private String[] draw = new String[]{R.drawable.t01 + "", R.drawable.t02 + "", R.drawable.t03 + ""};
    private static String  xzdraw;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_update_info);
        okHttpClient = new OkHttpClient();
        init();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            no.setText(bundle.getString("tno"));
            name.setText(bundle.getString("tname"));
            sex1.setText(bundle.getString("sex"));
            age1.setText(bundle.getString("age"));
            tel1.setText(bundle.getString("tel"));
        }
    }

    public void touxiang(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(TeacherUpdateInfo.this)
                .setTitle("选择头像")
                .setSingleChoiceItems(draw, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        xzdraw = draw[i];
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.create();
        dialog.show();
        Toast.makeText(TeacherUpdateInfo.this, xzdraw + "", Toast.LENGTH_SHORT).show();
    }

    //修改信息点击按钮
    public void updateTeacherInfo(View view) {
        String tno = no.getText() + "";
        String tname = name.getText() + "";
         String sex= sex1.getText() + "";
        int age=0;
         if (age1.getText()!=null&&!"".equals(age1.getText().toString())){
              age= Integer.parseInt(age1.getText()+"");
         }

        String tel= tel1.getText() + "";


        Teacher teacher = new Teacher(tno,tname,xzdraw,sex,age,tel,null);
        //转为json发送post请求
        String s = new Gson().toJson(teacher);
        final RequestBody requestBody = RequestBody.create(s, MediaType.parse("application/json"));
        Request build = new Request.Builder()
                .url(UrlBase.URL_BASE + "/teacher/updateTeacher")
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                ResultVo resultVo = new Gson().fromJson(string, ResultVo.class);
                if (resultVo.getCode()==1000){
                    Looper.prepare();
                    Toast.makeText(TeacherUpdateInfo.this,"修改成功 ",Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }else {
                    System.err.println("error");
                }
            }
        });

    }
public void back(View view){
        startActivity(new Intent(TeacherUpdateInfo.this,TeacherInfo.class));
}
    public void init() {
        no = findViewById(R.id.no);
        name = findViewById(R.id.name);
        sex1 = findViewById(R.id.sex);
        age1 = findViewById(R.id.age);
        tel1 = findViewById(R.id.tel);

    }
}
